// pages/addStore/index.js
const SERVER_BASE = ''
const computedBehavior = require('miniprogram-computed').behavior
const app = getApp()
Page({
  behaviors: [computedBehavior],
  data: {
    picUrl: [],
    managerPicUrl: [],
    longitude: '',
    latitude: '',
    user: ''
  },
  computed: {
    disabled(data) {
      const { address, storeName } = data
      if (!address || !storeName) {
        return true
      }
      return false
    }
  },
  takePhoto() {
    wx.chooseMedia({
      count: 1,
      mediaType: ["image"],
      sourceType: ["camera"],
      success: (res) => {
        this.setData({
          picUrl: res.tempFiles.map(file => ({
            url: file.tempFilePath,
            status: 'success'
          }))
        })
      },
      fail: (err) => {
        console.log(err)
        wx.showToast({
          title: '拍照失败',
          icon: 'error'
        })
      }
    })
  },
  // 预览图片
  previewImage() {
    if (this.data.picUrl.length > 0) {
      wx.previewImage({
        urls: [this.data.picUrl[0].url],
        current: this.data.picUrl[0].url
      })
    }
  },
  async submit() {
    const storeId = await insertStore({
      managerPicUrl: managerPicUrl[0] && managerPicUrl[0].status !== 'failed' && managerPicUrl[0].url,
      picUrl: picUrl[0] && picUrl[0].status !== 'failed' && picUrl[0].url
    })
    app.showToast('新增成功！')
    setTimeout(() => {
      wx.navigateTo({
        url: `/pages/qrCodePage/index?storeId=${storeId}`,
      })
    }, 2000)
  },
  onLoad({ params }) {
    const data = JSON.parse(decodeURIComponent(params))
    this.setData({
      ...data,
      user: app.globalData.user.name
    })
  }
})
